package solutions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.pi4.locutil.GeoPosition;
import org.pi4.locutil.MACAddress;
import org.pi4.locutil.io.TraceGenerator;
import org.pi4.locutil.trace.TraceEntry;

import solutions.MainAlgorithmRunner.TraceEstimater;

public class FingerPrintingNN extends BasePositioner implements TraceEstimater {

	List<EntryWithMACsAndAverages> offlineEntries = new ArrayList<EntryWithMACsAndAverages>();

	public FingerPrintingNN(TraceGenerator tg, List<Double> differences) {
		this.tg = tg;
		this.differences = differences;
	}

	@Override
	public void buildRadioMap(List<TraceEntry> offlineentries) {
		for (TraceEntry entry : offlineentries) {
			offlineEntries.add(new EntryWithMACsAndAverages(entry));
		}
	}

	public GeoPosition estimatePosition(TraceEntry onlineentry) {
		EntryWithMACsAndAverages onlineAverage = new EntryWithMACsAndAverages(
				onlineentry);
		for (EntryWithMACsAndAverages entry : offlineEntries) {
			for (MACAddress address : onlineAverage.addresses.keySet()) {
				if (entry.addresses.containsKey(address)) {
					double newDistance = PositionUtils.getDistance(
							onlineAverage.addresses.get(address),
							entry.addresses.get(address));
					entry.distance = newDistance;
				}
			}
		}
		Collections.sort(offlineEntries);

		return offlineEntries.get(0).getPosition();
	}

	@Override
	public List<EntryWithMACsAndAverages> getRadioMap() {
		// TODO Auto-generated method stub
		return null;
	}
}
